package com.gxan.web.servlet.recruitServlet;

import com.alibaba.fastjson.JSONObject;
import com.gxan.pojo.Resume;
import com.gxan.service.RecruitService;
import com.gxan.service.impl.RecruitServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/recruit/resume/searchResume")
public class SearchResumeServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");

        Map resultMap = new HashMap<>();
        try {
            String name = req.getParameter("name");
            String job_name = req.getParameter("job_name");
            String experience = req.getParameter("experience");
            String status = req.getParameter("status");
            String reg_time = req.getParameter("reg_time");

            Integer page =  Integer.parseInt(req.getParameter("page"));
            Integer limit =  Integer.parseInt(req.getParameter("limit"));

            RecruitService recruitService = new RecruitServiceImpl();
            List<Resume> resumeList = recruitService.searchResume(name,job_name,experience,status,reg_time,page,limit);

            resultMap.put("code",0);
            resultMap.put("msg","success");
            resultMap.put("count",recruitService.countSearchResume(name,job_name,experience,status,reg_time));
            //resultMap.put("count",9);
            resultMap.put("data",resumeList);
            // 需要将Java对象转换为json字符串对象才能在网络中进行传输
            resp.getWriter().write(JSONObject.toJSONString(resultMap));
        } catch (Exception e){
            e.printStackTrace();
            resultMap.put("code",-1);
            resultMap.put("msg","系统开小差了，请联系管理员");
            // 需要将Java对象转换为json字符串对象才能在网络中进行传输
            resp.getWriter().write(JSONObject.toJSONString(resultMap));
        }
        resp.getWriter().flush();
        resp.getWriter().close();
    }
}
